package Demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Auther: MFM
 * @Date: 2021/7/31 - 07 - 31 - 10:01
 * @version: 1.0
 */

public class Demo {
    public static <T> List<T> sort(List<T> list) {
        Stack<T> stack = new Stack<>();
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                T t1 = list.get(j);
                Comparable<T> comp = (Comparable<T>) t1;
                T t2 = list.get(i);
                if (comp.compareTo(t2) == 1) {
                    stack.push(t1);
                    list.remove(j);
                } else {
                    stack.push(t2);
                    list.remove(i);
                }
            }
        }

        while (stack.size() > 0) {
            list.add(stack.pop());
        }
        return list;
    }


    public static <T> void sortedList(List<T> list) {
        if (list.isEmpty()) {
            return;
        }
        List<T> tempList = new ArrayList<>();
        while (list.size() > 0) {
            tempList.add(list.remove(list.size() - 1));
        }

        list.add(tempList.remove(tempList.size() - 1));
        Stack<T> stack = new Stack<>();
        while (tempList.size() > 0) {
            T value = tempList.remove(tempList.size() - 1);
            Comparable<T> comp = (Comparable<T>) value;
            while (list.size() > 0) {
                T lastValue = list.get(list.size() - 1);
                if (comp.compareTo(lastValue) == 1) {
                    list.add(value);
                    break;
                } else {
                    stack.push(list.remove(list.size() - 1));
                }
            }
            if (list.isEmpty()) {
                list.add(value);
            }
            while (stack.size() > 0) {
                list.add(stack.pop());
            }
        }

    }


    public static void main(String[] args) {
        List<Integer> integers = new ArrayList<>();
        integers.add(12);
        integers.add(13);
        integers.add(2);
        integers.add(5);
        integers.add(7);
        integers.add(8);
        sortedList(integers);
        System.out.println(integers);
    }
}
